﻿namespace Lekce02
{
    using System;
    using System.Threading;
    using Microsoft.SPOT.Hardware;
    using SecretLabs.NETMF.Hardware.NetduinoPlus;

    public class Program
    {
        private static readonly OutputPort Led4 = new OutputPort(Pins.GPIO_PIN_D4,false);
        private static readonly OutputPort Led5 = new OutputPort(Pins.GPIO_PIN_D5, false);

        private static DateTime t = DateTime.Now;
        private static DateTime next = t.AddMilliseconds(500);

        public static void Main()
        {
            while (true)
            {
                // Blikej1();

                // Blikej2();

                Blikej3();
            }
        }

        private static void Blikej2()
        {
            SwapState();
            for (int i = 0; i < 20000; i++)
            {
            }
        }

        private static void Blikej1()
        {
            SwapState();
            Thread.Sleep(500);
        }

        private static void Blikej3()
        {
            if (DateTime.Now > next)
            {
                SwapState();
                next = DateTime.Now.AddMilliseconds(500);
            }
        }

        private static void SwapState()
        {
            Led4.Write(!Led4.Read());
            Led5.Write(!Led5.Read());
        }
    }
}
